﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimeAxis.Admin
{
    public partial class MyList : System.Web.UI.Page
    {
        public List<Models.Category> categorys = new List<Models.Category>();
        public List<Models.Doc> docs = new List<Models.Doc>();
        public string[] queryparam = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            Interfaces.ICategory iCategory = Impls.ImplCategory.GetInstance(db);
            categorys = iCategory.GetList();
            string param = Request.QueryString["param"];
            Models.Doc doc = new Models.Doc();
            Common.Web.Cookie cookie = new Common.Web.Cookie();

            doc.UsersId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            Models.Users user = Impls.ImplUsers.GetInstance(db).GetModel(doc.UsersId);
            if (doc.UsersId == Impls.ImplUsers.GetInstance(db).GetList().LastOrDefault().Id)
            {
                doc = new Models.Doc();

                if (!string.IsNullOrEmpty(param))
                {
                    queryparam = param.Split(new char[] { ',' });
                    string sql = "select a.* from Doc a,DocCategory b,Category c where a.Id=b.DocId and c.Id=b.CategoryId and c.Id in (" + param + ")";
                    System.Data.OleDb.OleDbDataReader sdr = db.ExecuteReader(sql);
                    while (sdr.Read())
                    {
                        Models.Doc td = new Models.Doc();
                        td.Id = Convert.ToInt32(sdr["Id"]);
                        td.Content = sdr["Content"].ToString();
                        td.Pdate = Convert.ToDateTime(sdr["Pdate"]);
                        td.Title = sdr["Title"].ToString();
                        td.UsersId = Convert.ToInt32(sdr["UsersId"]);
                        docs.Add(td);
                    }
                    sdr.Close();
                }
                else
                {
                    docs = Impls.ImplDoc.GetInstance(db).GetList();
                }

            }
            else if (!string.IsNullOrEmpty(doc.UsersId.ToString()))
            {
                if (!string.IsNullOrEmpty(param))
                {
                    queryparam = param.Split(new char[] { ',' });
                    string sql = "select a.* from Doc a,DocCategory b,Category c where a.Id=b.DocId and c.Id=b.CategoryId and a.UsersId=" + doc.UsersId.ToString() + " and c.Id in (" + param + ")";
                    System.Data.OleDb.OleDbDataReader sdr = db.ExecuteReader(sql);
                    while (sdr.Read())
                    {
                        Models.Doc td = new Models.Doc();
                        td.Id = Convert.ToInt32(sdr["Id"]);
                        td.Content = sdr["Content"].ToString();
                        td.Pdate = Convert.ToDateTime(sdr["Pdate"]);
                        td.Title = sdr["Title"].ToString();
                        td.UsersId = Convert.ToInt32(sdr["UsersId"]);
                        docs.Add(td);
                    }
                    sdr.Close();
                }
                else
                {
                    docs = Impls.ImplDoc.GetInstance(db).GetSomeObject(doc);
                }

            }
            else
            {
                docs = new List<Models.Doc>();
            }


            db.Commit();
            db.Dispose();
        }
    }
}